﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ElectronicWMSA.Comm.ClassFiles
{
    public class ValidationRegex
    {
        #region 除运算
        /// <summary>
        /// 除运算 
        /// </summary>
        /// <param name="a">被除数</param>  
        /// <param name="b">除数</param>  
        /// <param name="p">小数位数</param>  
        public static string Divide(int a, int b, int p)
        {
            System.Text.StringBuilder builder = new StringBuilder(p);
            int count = 0;
            builder.Append(a / b);
            a = a % b;
            if (a != 0)
                builder.Append(".");
            while (a != 0 && count < p)
            {
                a *= 10;
                builder.Append(a / b);
                a = a % b;
                count++;
            }
            Console.WriteLine(builder);
            return builder.ToString();
        }
        #endregion

        /// <summary>
        /// 正则表达式字符串
        /// </summary>
        public static string pattern;
        /// <summary>
        /// 公共方法
        /// </summary>
        /// <param name="pattern">正则表达式</param>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>Bool</returns>
        private static bool PublicMethod(string pattern, string validteString)
        {
            Regex reg = new Regex(pattern);
            Match m = reg.Match(validteString);
            return m.Success;
        }

        /// <summary>
        /// 验证正浮点数  
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidteDataFloat(string validteString)
        {
            pattern = @"^\+?[0-9]*(\.[0-9]*)?([Ee][-+]?[0-9]+)?$";
            return PublicMethod(pattern, validteString);
        }
        /// <summary>
        /// 验证正整数
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidteData(string validteString)
        {
            pattern = "^[+]?\\d+$";
            return PublicMethod(pattern, validteString);
        }

        /// <summary>
        /// 验证正负整数
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidatePlusMinus(string validteString)
        {
            pattern = @"^\+?[0-9]*(\.[0-9]*)?([Ee][-+]?[0-9]+)?$";
            return PublicMethod(pattern, validteString);
        }

        /// <summary>
        /// 验证字母和数字
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool VadidateDataLetter(string validteString)
        {
            pattern = "^[a-zA-Z0-9]+$";
            return PublicMethod(pattern, validteString);
        }
        /// <summary>
        /// 验证中文字符
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidateChineseChar(string validteString)
        {
            pattern = "^[\u4e00-\u9fa5]+$";
            return PublicMethod(pattern, validteString);
        }
        /// <summary>
        /// 验证时间-【H:mm:ss】
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidateTime(string validteString)
        {
            pattern = "^[0-9]{1,2}:[0-9]{2}(:[0-9]{2})?$";
            return PublicMethod(pattern, validteString);
        }

        /// <summary>
        /// 验证固定电话
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidateTel(string validteString)
        {
            pattern = "^([0-9]{3,4}-)?[0-9]{7,8}$";
            return PublicMethod(pattern, validteString);
        }

        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidatePhone(string validteString)
        {
            pattern = "^(13|15|18)[0-9]{9}$";
            return PublicMethod(pattern, validteString);
        }

        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidateEmail(string validteString)
        {
            pattern = "^\\w+([+-.]\\w+)*@\\w+([-.]\\w+)*$";
            return PublicMethod(pattern, validteString);
        }
        /// <summary>
        /// 验证邮编
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool ValidatePostCode(string validteString)
        {
            pattern = "^\\d{6}$";
            return PublicMethod(pattern, validteString);
        }

        /// <summary>
        ///   验证输入字符串为网络地址
        /// </summary>
        /// <param name="P_str_naddress"> 输入字符串 </param>
        /// <returns> 返回一个bool类型的值 </returns>
        public static bool validateNAddress(string P_str_naddress)
        {
            return Regex.IsMatch(P_str_naddress, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
        }

        /// <summary>
        /// Double类型
        /// </summary>
        public static bool IsNumeric(string number)
        {
            try
            {
                double.Parse(number);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 只允许输入数字\退格键
        /// </summary>
        public static void OnlyInputNumber(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                {
                    e.Handled = true;
                }
            }
        }


        /// <summary>
        /// 验证字母和数字和-
        /// </summary>
        /// <param name="validteString">待验证字符串</param>
        /// <returns>bool</returns>
        public static bool VadidateDataLetter01(string validteString)
        {
            pattern = "^[a-zA-Z0-9-]+$";
            return PublicMethod(pattern, validteString);
        }

        /// <summary>
        /// 大写字母,空格，逗号，横杠
        /// </summary>
        /// <param name="validteString"></param>
        /// <returns></returns>
        public static bool CapitallettersAndSpaces(string validteString)
        {
            //pattern = "^[A-Z ]+$";
                pattern = "^[A-Z0-9 ,-]+$";
                return PublicMethod(pattern, validteString);

        }


    }
}
